package com.runen.infinitemovement.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.tool.ActivityUtils;
import com.runen.infinitemovement.tool.StatusBarUtil;
import com.runen.infinitemovement.tool.Utils;
import com.runen.rxnetlibrary.utils.AlertUtils;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;


/**
 * Create by LiaoDuanHong
 * Create 2020/08/26 14:52
 */
public abstract class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBar();
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        if (getLayoutId() == 0) {
            throw new IllegalArgumentException("you must return a right resource id");
        } else {
            setContentView(getLayoutId());
        }
        initParam();
        initView(savedInstanceState);
        ActivityUtils.getInstance().addActivity(this);
    }

    protected abstract int getLayoutId();

    protected abstract void initView(@Nullable Bundle savedInstanceState);

    public void initParam() {
        if (Utils.hasEventBus(this.getClass())) {
            EventBus.getDefault().register(this);
        }
    }

    /**
     * @return activity的布局文件
     */

    private void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (isUseFullScreenMode()) {
                StatusBarUtil.transparencyBar(this);
            } else {
                StatusBarUtil.setStatusBarColor(this, getStatusBarColor());
            }

            if (isUseBlackFontWithStatusBar()) {
                StatusBarUtil.setLightStatusBar(this, true, isUseFullScreenMode());
            }
        }
    }

    /**
     * 是否设置成透明状态栏，即就是全屏模式
     */
    protected boolean isUseFullScreenMode() {
        return false;
    }

    /**
     * 更改状态栏颜色，只有非全屏模式下有效
     */
    protected int getStatusBarColor() {
        return R.color.white;
    }

    /**
     * 是否改变状态栏文字颜色为黑色，默认为黑色
     */
    protected boolean isUseBlackFontWithStatusBar() {
        return true;
    }

    public void jumpActivity(Class<?> clazz) {
        startActivity(new Intent(this, clazz));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Utils.hasEventBus(this.getClass())) {
            EventBus.getDefault().unregister(this);
        }
        ActivityUtils.getInstance().finishActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 选用AUTO页面采集模式
        MobclickAgent.onResume(this);
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    public void showDialog() {
        showDialog("加载中...",true);
    }

    public void showDialog(String title,boolean isCancel) {
        AlertUtils.showProgressDialog(this,"",title,isCancel);
    }

    public void dismissDialog() {
        AlertUtils.closeProgressDialog();
    }
}
